/**
 * 文件名称:  TBcMsgController.java
 * 文件序号:  序号
 * 内容描述:  文件简单描述，多行换行书写
 * 版权声明:  山东鸿蝶信息技术有限公司
 * 创建用户:  术洪超
 * 创建日期:  2017/2/13 10:00
 * 修改用户:  无
 * 修改日期:  无
 * 修改说明:  无
 * 文件版本:  V1.0
 * 文件路径:
 */
package com.hongdee.base.msg.controller;


import com.alibaba.druid.util.StringUtils;
import com.hongdee.base.entity.ResultEntity;
import com.hongdee.base.msg.entity.TBcMsg;
import com.hongdee.base.msg.service.ITBcMsgService;
import com.hongdee.common.util.EntityUtils;
import com.hongdee.common.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 名  称:TBcMsgController
 * 类  型: class
 * 描  述: 用一句话描述类的作用
 * 路  径:com.hongdee.base.msg.controller
 * 作  者:术洪超
 * 日  期:2017/2/13 11:00
 */

@RestController
@RequestMapping("/base/msg")
public class TBcMsgController {

    @Autowired
    private ITBcMsgService iTBcMsgService;


    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param start 起始记录
     * @param limit 页面大小
     * @return Map
     * @throws Exception
     * @desc 通过 JDBCDao分页查询
     * @author 术洪超
     */
    @RequestMapping("/page")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "start", defaultValue = "") String start,
                             @RequestParam(name = "limit", defaultValue = "") String limit

    ) throws Exception {
        String companyId = TokenUtil.getCompanyId(uid);
        Map map = iTBcMsgService.page(companyId, start, limit);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;

//        ResultEntity res = new ResultEntity();
//        res.setData(map);
//        res.setMsg("查询成功！");
//        res.setSuccess(true);
//        return res;
    }

    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param id    主键 ID
     * @return ResultEntity
     * @throws Exception
     * @desc 查询详情
     * @author 张国凯
     */
    @RequestMapping("/info")
    @ResponseBody
    public ResultEntity info(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        if (StringUtils.isEmpty(id)) {
            throw new Exception("请选择短信");
        }
        Map map = iTBcMsgService.info(id);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }


    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param id    主键 ID
     * @return ResultEntity
     * @throws Exception
     * @desc 启用品牌
     * @author 张国凯
     */
    @RequestMapping("/start")
    @ResponseBody
    public ResultEntity start(@RequestParam(name = "uid", defaultValue = "") String uid,
                              @RequestParam(name = "token", defaultValue = "") String token,
                              @RequestParam(name = "id", defaultValue = "0") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        iTBcMsgService.update(id, "1");
        res.setSuccess(true);
        res.setMsg("启用成功");
        return res;
    }

    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param id    主键 ID
     * @return ResultEntity
     * @throws Exception
     * @desc 停用品牌
     * @author 张国凯
     */
    @RequestMapping("/stop")
    @ResponseBody
    public ResultEntity stop(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "0") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        iTBcMsgService.update(id, "0");
        res.setSuccess(true);
        res.setMsg("停用成功！");
        return res;
    }

    /**
     * @param data 要保存的短信模板实体
     * @return ResultEntity
     * @throws Exception
     * @desc 保存添加或者修改的实体内容，通过主键判断
     * @author 术洪超
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody TBcMsg data) throws Exception {
        // System.out.println(uid + "----------");

        //判断必填项
        EntityUtils.valid(data);
        String oid= TokenUtil.getCompanyId(uid);
        data.setCompanyId(oid);
        TBcMsg msg = iTBcMsgService.saveOrUpdate(data);
        ResultEntity res = new ResultEntity();
        res.setData(msg);
        res.setSuccess(true);
        res.setMsg("保存成功！");
        return res;
    }

    /**
     * 获得匹配后的短信内容
     * @param uid
     * @param token
     * @param typeCode
     * @param map
     * @return
     * @throws Exception
     */
    @RequestMapping("/getMsg")
    @ResponseBody
    public ResultEntity getMsg(@RequestParam(name = "uid", defaultValue = "") String uid,
                               @RequestParam(name = "token", defaultValue = "") String token,
                               @RequestParam(name = "typeCode", defaultValue = "")String typeCode,
                               @RequestBody Map<String,Object> map) throws Exception {
        ResultEntity res = new ResultEntity();
        String companyId = TokenUtil.getCompanyId(uid);
        String msg=iTBcMsgService.getMsgContent(companyId,typeCode,map);
        if(msg!=null) {
            res.setData(msg);
            res.setSuccess(true);
            res.setMsg("短信查询成功！");
        }else{
            res.setData("");
            res.setSuccess(false);
            res.setMsg("短信模板不存在，请联系管理员!!");
        }
        return res;
    }

}
